package com.dingdang.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dingdang.data.Result;
import com.dingdang.dto.UserDTO;
import com.dingdang.em.ResultEnum;
import com.dingdang.entity.TbUser;
import com.dingdang.mapper.TbUserMapper;
import com.dingdang.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 董书乾
 * @since 2021-12-17
 */
@RestController
@RequestMapping("/login")
@Api(value = "单点登录逻辑")
public class LoginController {
    @Resource
    private TbUserMapper tbUserMapper;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private JwtUtil jwtUtil;
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    //获取验证码
    @PostMapping("/getCode")
    @ApiOperation(value = "获取验证码")
    @ApiImplicitParam(name = "phone", value = "账号名", paramType = "query", required = true)
    public Result getCode(@RequestParam("phone") String phone){
//       通过mq发送队列消息,发送短信
        rocketMQTemplate.convertAndSend("smsTopic",phone);
        return Result.response(ResultEnum.CODE_SUCCESS);
    }

//    登录注册验证
    @PostMapping("/loginCheck")
    @ApiOperation(value = "单点登录接口")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "phone", value = "账号名", paramType = "query", required = true),
                    @ApiImplicitParam(name = "code", value = "验证码", paramType = "query", required = true)})
    public Result login(@RequestParam("phone") String phone, @RequestParam("code") String code) {
//        先校验验证码
        String str = (String) redisTemplate.boundValueOps(phone).get();
        if (!ObjectUtils.equals(str, code)) {
            return Result.response(ResultEnum.CODE_ERROR);
        }
        TbUser user = new TbUser();
        user.setPhone(phone);
        UserDTO dto = new UserDTO();

        if (StringUtils.isEmpty(phone)) {
            return Result.response(ResultEnum.ARGS_ERROR);
        }
        QueryWrapper Wrapper = new QueryWrapper<TbUser>();
        Wrapper.like("phone", phone);
        List<TbUser> userList = tbUserMapper.selectList(Wrapper);
//        如果没有创建
        if (userList.size() == 0 || userList.get(0) == null) {
            tbUserMapper.insert(user);
        }
        BeanUtils.copyProperties(userList.get(0), dto);
        String token = jwtUtil.createJwt(dto.getUid().toString(), JSONObject.toJSONString(dto), "");
        return Result.response(ResultEnum.SUCCESS).setData(token);
    }

}
